<?php

namespace App\Observers;

use App\Models\Topic;
use App\Jobs\TranslateSlug;
use App\Handlers\SlugTranslateHandler;
// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class TopicObserver{
    public function saving(Topic $topic){
        // XSS 过滤
        $topic->body = clean($topic->body,'user_topic_body');
        // 生成话题摘录
        $topic->excerpt = make_excerpt($topic->body);

    }

    public function saved(Topic $topic){
        // 如 slug 字段无内容，即使用翻译器对title 进行翻译
        if(!$topic->slug){
            // 推送任务到队列
            dispatch(new TranslateSlug($topic));
            // $topic->slug = app(SlugTranslateHandler::class)->translate($topic->title);
        }
    }

    // 删除回复
    public function deleted(Topic $topic){
        \DB::table('replies')->where('topic_id',$topic->id)->delete();
    }
}